package com.boilermaker.lotterysystem.controller;

import com.boilermaker.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.boilermaker.lotterysystem.common.exception.ControllerException;
import com.boilermaker.lotterysystem.common.pojo.CommonResult;
import com.boilermaker.lotterysystem.common.utils.JacksonUtil;
import com.boilermaker.lotterysystem.controller.param.PageParam;
import com.boilermaker.lotterysystem.controller.param.PrizeParam;
import com.boilermaker.lotterysystem.controller.vo.PrizeListVO;
import com.boilermaker.lotterysystem.service.PictureService;
import com.boilermaker.lotterysystem.service.PrizeService;
import com.boilermaker.lotterysystem.service.dto.PageListDTO;
import com.boilermaker.lotterysystem.service.dto.PrizeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
public class PrizeController {

    /**
     * 奖品模块注入
     */
    @Autowired
    private PrizeService prizeService;

    /**
     * 创建奖品
     *
     * @param param:   奖品属性
     * @param picture: 奖品图
     * @return: 统一返回格式的奖品 ID
     */
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(@Validated @RequestPart("param") PrizeParam param,
                                          @RequestPart("prizePic") MultipartFile picture) {

        log.info("createPrize PrizeParam:{}", JacksonUtil.writeValueAsString(param));

        Long id = prizeService.createPrize(param, picture);

        return CommonResult.success(id);
    }

    /**
     * 获取奖品列表，支持翻页功能
     *
     * @param param
     * @return
     */
    @RequestMapping("/prize/find-list")
    public CommonResult<PrizeListVO> findPrizeList(PageParam param) {

        log.info("findPrizeList PageParam:{}", JacksonUtil.writeValueAsString(param));

        PageListDTO<PrizeDTO> pageListDTO = prizeService.findPrizeList(param);

        return CommonResult.success(convertPrizeListDtoToVo(pageListDTO));
    }

    /**
     * 将 DTO 转换为 VO。DTO 仅用于传输业务数据，而 VO 站在前端需求角度封装数据。
     *
     * @param pageListDTO
     * @return
     */
    private PrizeListVO convertPrizeListDtoToVo(PageListDTO<PrizeDTO> pageListDTO) {

        if (pageListDTO == null) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }

        PrizeListVO prizeListVO = new PrizeListVO();
        prizeListVO.setTotal(pageListDTO.getTotal());
        List<PrizeDTO> list = pageListDTO.getRecords();

        prizeListVO.setRecords(
                list.stream().map(prizeDTO -> { // 将 List<PrizeDTO> 映射为 List<PrizeListVO.PrizeVO>
                    PrizeListVO.PrizeVO vo = new PrizeListVO.PrizeVO();

                    vo.setPrizeId(prizeDTO.getPrizeId());
                    vo.setPrizeName(prizeDTO.getPrizeName());
                    vo.setDescription(prizeDTO.getDescription());
                    vo.setImageUrl(prizeDTO.getImageUrl());
                    vo.setPrice(prizeDTO.getPrice());

                    return vo;
                }).collect(Collectors.toList())
        );

        return prizeListVO;
    }
}
